package com.credit.jumu.calculator.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.BuildConfig;
import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.base.BaseActivity;
import com.credit.jumu.calculator.calculatorapp.CalculatorApp_;
import com.credit.jumu.calculator.fragment.FindFragment;
import com.credit.jumu.calculator.fragment.FindFragment_;
import com.credit.jumu.calculator.fragment.HomeFragment;
import com.credit.jumu.calculator.fragment.HomeFragment_;
import com.credit.jumu.calculator.utils.DataSharedPreference;
import com.credit.jumu.calculator.utils.JsonUtils;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.MD5Encoder;
import com.credit.jumu.calculator.utils.MonitorOnBack;
import com.credit.jumu.calculator.utils.SignUtil;
import com.credit.jumu.calculator.utils.SystemUtil;
import com.credit.jumu.calculator.utils.TypeUtils;
import com.credit.jumu.calculator.utils.UrlUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity {

	@ViewById
	ImageView hometabicon, findtabicon;
	@ViewById
	TextView hometabtext, findtabtext, barheight;
	@ViewById
	FrameLayout allfragment;
	Boolean isHome = true;
	Context context;
	FragmentManager fm;
	//	FragmentTransaction ft;
	public HomeFragment homeFragment;
	public FindFragment findFragment;
	private DataSharedPreference ds;
	private MonitorOnBack monitorOnBack;

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@AfterViews
	@Override
	public void initView() {
		monitorOnBack = new MonitorOnBack();
		context = this;
		RelativeLayout.LayoutParams Params1 = (RelativeLayout.LayoutParams) barheight.getLayoutParams();
		Params1.height = getNavigationBarHeight();
		barheight.setLayoutParams(Params1);
		toAllWindow();
		fm = getSupportFragmentManager();
		showFragment(0);
		getds();
		initUv();
		initSet();
		initUserInfo();
	}

	private void initUv() {
		//uv
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CalculatorApp_.intefaceType);
		mapSign.put("source", CalculatorApp_.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CalculatorApp_.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CalculatorApp_.intefaceType);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CalculatorApp_.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		OkGo.<String>post(UrlUtil.UPLOADUVSURFIX)//
				.tag(this)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								LogUtils.println("zhangyiyionSuccessUV------" + response.body());
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionErrorUV------" + response.body());
							}
						}
				);
	}

	private void initSet() {
		//获取系统设置
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CalculatorApp_.intefaceType);
		mapSign.put("source", CalculatorApp_.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CalculatorApp_.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CalculatorApp_.intefaceType);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CalculatorApp_.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		OkGo.<String>post(UrlUtil.URLLISTSURFIX)//设置信息
				.tag(this)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								LogUtils.println("zhangyiyionSuccessSet------" + response.body());
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									JSONObject data = TypeUtils.getJsonObject(responseJson, "data");
									JSONObject versions = TypeUtils.getJsonObject(data, "appVersions");

									String version = TypeUtils.getJsonString(versions, "version", "0");
									String isUpdate = TypeUtils.getJsonString(versions, "isUpdate", "0");
									String appContent = TypeUtils.getJsonString(versions, "appContent", "");
									String downUrl = TypeUtils.getJsonString(versions, "downUrl", "0");

									if (null == ds) return;
									ds.setAboutUrl(TypeUtils.getJsonString(data, "aboutUrl", ""));
									ds.setRegisterXieyiUrl(TypeUtils.getJsonString(data, "registerXieyiUrl", ""));
									//ds.setRedirectUrl(TypeUtils.getJsonString(data, "redirectUrl", ""));
								}
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionErrorSet------" + response.body());
							}
						}
				);
	}

	private void initUserInfo() {
		//获取详细信息
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		if (ds.getIsLog()) {
			mapSign.put("accountId", ds.getAccountId());
			mapSign.put("deviceCode", ds.getUUID());
			mapSign.put("intefaceType", CalculatorApp_.intefaceType);
			mapSign.put("source", CalculatorApp_.source);
			mapSign.put("terminalId", "A");
			mapSign.put("timeStamp", SystemUtil.getSystemTime());
			mapSign.put("token", ds.getToken());
			mapSign.put("version", BuildConfig.VERSION_NAME);
			mapSign.put("key", CalculatorApp_.key);
		} else {
			mapSign.put("deviceCode", ds.getUUID());
			mapSign.put("intefaceType", CalculatorApp_.intefaceType);
			mapSign.put("source", CalculatorApp_.source);
			mapSign.put("terminalId", "A");
			mapSign.put("timeStamp", SystemUtil.getSystemTime());
			mapSign.put("version", SystemUtil.getVersionCode(this));
			mapSign.put("key", "e83fgehkJuHeXinYongContosedce9ff");
		}

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", ds.getAccountId());
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CalculatorApp_.intefaceType);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CalculatorApp_.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", ds.getToken());
		mapPramas.put("version", BuildConfig.VERSION_NAME);

		OkGo.<String>post(UrlUtil.USERINFOSURFIX)//设置信息
				.tag(this)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								LogUtils.println("zhangyiyioninitUserInfo------1111111" + response.body());
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									JSONObject data = TypeUtils.getJsonObject(responseJson, "userInfo");
									if (null == ds) return;
									ds.setAboutUrl(TypeUtils.getJsonString(data, "aboutUrl", ""));
									ds.setRegisterXieyiUrl(TypeUtils.getJsonString(data, "registerXieyiUrl", ""));
									ds.setRedirectUrl(TypeUtils.getJsonString(data, "redirectUrl", ""));
								}
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionerroinitUserInfo------1111111" + response.body());
							}
						}
				);
	}


	private void getds() {
		if (null == ds) {
			ds = new DataSharedPreference(this);
		}
		ds.setUUID(SystemUtil.getAndroidId(getApplicationContext()));
	}


	@Click(R.id.hometablayout)
	public void hometabClick() {
		hideKeyboard();
		if (isHome) {
			return;
		}
		hometabicon.setImageResource(R.mipmap.click_home);
		findtabicon.setImageResource(R.mipmap.unclick_find);
		hometabtext.setTextColor(ContextCompat.getColor(context, R.color.tabtextbg));
		findtabtext.setTextColor(ContextCompat.getColor(context, R.color.tabtextunchoosebg));
		isHome = true;
		showFragment(0);
	}

	@Click(R.id.findtablayout)
	public void findtabClick() {//发现点击
		hideKeyboard();
		if (ds.getIsLog()) {
			if (!isHome) {
				return;
			}
			monitorOnBack = new MonitorOnBack();
			hometabicon.setImageResource(R.mipmap.unclick_home);
			findtabicon.setImageResource(R.mipmap.click_find);
			hometabtext.setTextColor(ContextCompat.getColor(context, R.color.tabtextunchoosebg));
			findtabtext.setTextColor(ContextCompat.getColor(context, R.color.tabtextbg));
			isHome = false;
			showFragment(1);
		} else {
			Intent intent = new Intent(MainActivity.this, SignActivity_.class);
			startActivity(intent);
		}
	}

	public void showFragment(int index) {
		monitorOnBack = new MonitorOnBack();
		FragmentTransaction ft = fm.beginTransaction();
		hideFragment(ft);
		switch (index) {
			case 0:
				if (homeFragment == null) {
					homeFragment = new HomeFragment_();
					ft.add(R.id.allfragment, homeFragment);
				} else {
					ft.show(homeFragment);
				}
				break;
			case 1:
				if (findFragment == null) {
					findFragment = new FindFragment_();
					ft.add(R.id.allfragment, findFragment);
				} else {
					ft.show(findFragment);
				}
				break;
		}
		ft.commit();
	}

	public void hideFragment(FragmentTransaction fragmentTransaction) {
		if (null != homeFragment) {
			fragmentTransaction.hide(homeFragment);
		}
		if (null != findFragment) {
			fragmentTransaction.hide(findFragment);
		}
	}

	@Override
	public void onBackPressed() {
		if (isHome) {
			monitorOnBack.onBack(this);
		} else {
			if (!CalculatorApp_.canGoBack) {//默认为否,便是不在web的二级页面
				monitorOnBack.onBack(this);
			} else {
				if (null != findFragment) {
					findFragment.goBack();
				}
			}
		}
	}
}

